package com.mike.service.impl;

import com.mike.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Date 2021/11/29 16:53
 * @Created by mike
 */
@Service
public class MailServiceImpl implements MailService {
    @Autowired
    JavaMailSender mailSender;

    @Override
    @Async
    public Boolean sendAuthCode(String code ,String to){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("mikelau@126.com");
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject("验证码");
        simpleMailMessage.setText(code);
        try {
            System.out.println("发送邮件 : " + simpleMailMessage);
            mailSender.send(simpleMailMessage);
        }catch (MailException e){
            e.printStackTrace();
            System.out.println("发送邮件失败");
            return false;
        }
        System.out.println("发送邮件成功");
        return true;
    }
}
